package com.daocao.ds.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.daocao.ds.R;
import com.daocao.ds.api.model.DealDetailModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class LoanerInfoAdapter extends ItemAdapter<DealDetailModel.DatasBean.BaseInfoBean.KeyVauleBean, RecyclerView.ViewHolder> {


    private Context context;

    private final int base_info_expand = 2;
    private final int base_info_common = 3;

    public LoanerInfoAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case base_info_expand:
                return new BaseExpandHolder(inflate(R.layout.item_loaner_base_expand, parent));
            case base_info_common:
                return new BaseCommonHolder(inflate(R.layout.item_loaner_base_common, parent));
            default:
                return new DefaultHolder(inflate(R.layout.item_loaner_base_common, parent));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DealDetailModel.DatasBean.BaseInfoBean.KeyVauleBean item = getItem(position);
        if (holder instanceof BaseExpandHolder) {
            ((BaseExpandHolder) holder).txtLeft.setText(item.key);
            ((BaseExpandHolder) holder).txtRight.setText(item.value);
            ((BaseExpandHolder) holder).layoutTotal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (((BaseExpandHolder) holder).list.getVisibility() == View.VISIBLE) {
                        ((BaseExpandHolder) holder).list.setVisibility(View.GONE);
                        ((BaseExpandHolder) holder).imgExpand.setImageResource(R.mipmap.img_info_down);
                    } else {
                        ((BaseExpandHolder) holder).list.setVisibility(View.VISIBLE);
                        ((BaseExpandHolder) holder).imgExpand.setImageResource(R.mipmap.img_info_up);
                    }
                }
            });

            LoanerInfoItemAdapter adapter = new LoanerInfoItemAdapter(context);
            ((BaseExpandHolder) holder).list.setAdapter(adapter);
            ((BaseExpandHolder) holder).list.setNestedScrollingEnabled(false);
            adapter.update(item.sub);

        } else if (holder instanceof BaseCommonHolder) {
            ((BaseCommonHolder) holder).txtLeft.setText(item.key);
            ((BaseCommonHolder) holder).txtRight.setText(item.value);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position).sub != null) {
            return base_info_expand;
        } else {
            return base_info_common;
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {

        private DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BaseExpandHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_left)
        TextView txtLeft;
        @BindView(R.id.img_expand)
        ImageView imgExpand;
        @BindView(R.id.txt_right)
        TextView txtRight;
        @BindView(R.id.layout_total)
        FrameLayout layoutTotal;
        @BindView(R.id.list)
        RecyclerView list;

        private BaseExpandHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BaseCommonHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_left)
        TextView txtLeft;
        @BindView(R.id.txt_right)
        TextView txtRight;

        private BaseCommonHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess();
    }
}
